from typing import Annotated

from fastapi import APIRouter, Depends, Form
from fastapi.security import OAuth2PasswordRequestForm

from app.core.security import oauth2_scheme
from app.deps.controller_deps import AuthControllerDep
from app.schemas.result_schema import ResultModel
from app.schemas.user_schema import UserLogin
from app.schemas.user_schema import UserLoginOut

auth_router = APIRouter()

@auth_router.post("/login", summary="用户登录")
async def login(login_in: Annotated[UserLogin,Form()],  controller: AuthControllerDep) -> ResultModel[UserLoginOut] | None:
    return await controller.login(login_in)

@auth_router.post("/login_oauth2", summary="用户登录", description="用于swagger登录携带Token")
async def login_oauth2(form_data: Annotated[OAuth2PasswordRequestForm, Depends()], controller: AuthControllerDep) -> UserLoginOut| None:
    return (await controller.login(UserLogin.model_validate({"account":form_data.username,"password":form_data.password}))).data

@auth_router.post("/refresh_token", summary="刷新Token")
async def refresh_token(refresh_token: Annotated[str,Form()],controller: AuthControllerDep,token: Annotated[str, Depends(oauth2_scheme)]) -> ResultModel[UserLoginOut]  | None:
    return await controller.refresh_token(refresh_token=refresh_token,token=token)

@auth_router.post("/refresh_token_oauth2", summary="刷新Token")
async def refresh_token_oauth2(refresh_token: Annotated[str,Form()],controller: AuthControllerDep,token: Annotated[str, Depends(oauth2_scheme)]) -> UserLoginOut | None:
    return (await controller.refresh_token(refresh_token=refresh_token,token=token)).data

@auth_router.get("/wechat_login", summary="微信登录")
async def wechat_login(controller: AuthControllerDep):
    return await controller.login_by_wechat()
